package.cpath = "./skynet/luaclib/?.so;".. package.cpath
package.path = "./skynet/lualib/?.lua;./app/common/lib/?.lua;./testclient/?.lua;" .. package.path

if _VERSION ~= "Lua 5.3" then
	error "Use lua 5.3"
end

local socket = require "client.websocket"
local protobuf = require "protobuf"
local parser = require "parser"

local md5 = require "md5"

parser.register("./app/proto/primary.proto")

local platform2login_key = "p2l_salt"

local decode = require "protopack"

local fd = assert(socket.connect("127.0.0.1", 8191))

local function send_package(fd, pack)
	-- 使用了websocket ，会自己组织帧格式的
	socket.send(fd, pack)
end

local function recv_package()
	local r , istimeout= socket.recv(fd, 100)
	if not r then
		return nil
	end
	if r == ""  and (not istimeout ) then
		error "Server closed"
	end
	return r
end


local function send_request(msgname, msg)
    if msgname ~= "heart" then
        print(msgname)
	    print(msg)
    end
	
	local str = decode.c2spack(protobuf, msgname, msg)
	send_package(fd, str)
end

local last = ""

local function print_request(name, args)
	print("REQUEST", name)
	if args then
		for k,v in pairs(args) do
			print(k,v)
		end
	end
end

local function print_response(msghead, msgbody)
    if msghead.msgname == "heartR" then
        return
    end

    print('------ begin response --------')
	if msghead then
		for k,v in pairs(msghead) do
			print(k,v)
			if v == "authR" and msgbody then
                gate_ip = msgbody.gateip
                rid = msgbody.rid  
                gateport = msgbody.gateport     
                expiretime = msgbody.expiretime
                gatetoken = msgbody.gatetoken
                username = msgbody.username
                cpass = msgbody.cpass
                fd = assert(socket.connect(gate_ip, gateport))
                if fd then
                    send_request("entergame", {rid=rid, uid=uid, gatetoken=gatetoken,expiretime = expiretime,agencyrid = agencyrid, clientver = "0000"})
                end
            end
		end
	end
	if msgbody then
		for k,v in pairs(msgbody) do
			print(k,v)
			if type(v) == "table" then
				print("-- table: ", k )
				for i,j in pairs(v) do
					print("  --", i,j)
                    if type(j) == "table" then
                        print("------ subtable: ", i)
                        for kk,vv in pairs(j) do
                            print("     ----",kk,vv)
                        end
                        
                    end
				end
			end
		end
	end
	print('------ end response --------')
end

local function dispatch_package()
	while true do
		local v
		v = recv_package()
		if not v  or v == "" then
			break
		end
		print_response(decode.s2cunpack(protobuf, v))
	end
end

local function gentoken(username)
    local tokentime = os.time()
    local newtoken = md5.sumhexa(username .. tokentime .. platform2login_key ) .. tokentime
    return newtoken
end

math.randomseed(os.time())
math.random()

while true do
	dispatch_package()
	local cmd = socket.readstdin()
	if cmd then
        local code = string.sub(cmd,4) or nil
		if cmd == "awx" then
            local newtoken = gentoken("xxid_9999")
			send_request("authwx", {username = "xxid_9999", refresh_token = "46634e1c0013cdea0172e1fab15bc94b1504418301", rolename = "abc", logintoken = newtoken})
		elseif cmd == "aur" then
			local newtoken = gentoken("xxid_9999")
			send_request("authuser", {username = "xxid_9999", cpass = md5.sumhexa("xxid_9999abcd"), logintoken = newtoken})
		elseif cmd == "agt" then
            local username = "46634e1c0013cdea0172" .. math.random(100000,999999)
			local newtoken = gentoken(username)
			send_request("authguest", {username = username, logintoken = newtoken})
        elseif cmd == "ago" then
            local username = "17612341234"
			local newtoken = gentoken(username)
			send_request("authother", {username = username, cpass = md5.sumhexa(username .. "qwerty"), logintoken = newtoken})
        elseif cmd == "gvc" then
            send_request("getverificationcode", {phonenum = "17122333344"})
        elseif tonumber(code) and string.sub(cmd,1,3) == "aup" then
            send_request("phoneauth", {phonenum = "17122333344", verification_code = tonumber(code), realname = "张三", cpass = md5.sumhexa("17122333344abcd")})
        end
	else
		socket.usleep(100)
	end
end